package com.lr.students.controller;

import com.lr.students.entry.Result;
import com.lr.students.entry.Code;
import com.lr.students.model.Classess;
import com.lr.students.service.ClassessService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;



@RestController
@RequestMapping("/classess")
@CrossOrigin
public class ClassessController {

    @Autowired
    private ClassessService classessService;


    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Classess classess, @PathVariable  int page, @PathVariable  int size){

        PageInfo<Classess> pageInfo = classessService.findPage(classess, page, size);
        return new Result(true, Code.OK,"查询成功",pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){

        PageInfo<Classess> pageInfo = classessService.findPage(page, size);
        return new Result<PageInfo>(true, Code.OK,"查询成功",pageInfo);
    }


    @GetMapping("/{Name}")
    public Result<Classess> findById(@PathVariable String Name){

        Classess classess = classessService.findByName(Name);
        return new Result<Classess>(true, Code.OK,"查询成功", classess);
    }


    @GetMapping
    public Result<List<Classess>> findAll(){

        List<Classess> list = classessService.findAll();
        return new Result<List<Classess>>(true, Code.OK,"查询成功",list) ;
    }


    @PostMapping(value = "/search" )
    public Result<List<Classess>> findList(@RequestBody(required = false) Classess classess){

        List<Classess> list = classessService.findList(classess);
        return new Result<List<Classess>>(true, Code.OK,"查询成功",list);
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){

        classessService.delete(id);
        return new Result(true, Code.OK,"删除成功");
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody Classess classess, @PathVariable Integer id){

        classess.setId(id);

        classessService.update(classess);
        return new Result(true, Code.OK,"修改成功");
    }


    @PostMapping
    public Result add(@RequestBody Classess classess){

        classessService.add(classess);
        return new Result(true, Code.OK,"添加成功");
    }

}
